/* ----------------------------------------------------------------------
 *	Shadow Quest
 *	Copyright (C) 2009 Prageeth Silva <prageeth@thenewcoders.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  ----------------------------------------------------------------------
 */
package units;

import java.util.ArrayList;
import map.World;

import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

/** Represents the Monster character.
 * 
 */
public class Fighter extends FightingUnit {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /** Create a new Monster object (overload).
     * @param type The Monster type
     * @param width The width of the monster
     * @param height The height of the monster
     */
    public Fighter(World world, int team, String name)
            throws SlickException {

        // temporarily make the super class and then replace the images
        super(world, team);

        name = name.toLowerCase();
        this.setDirectoryPath(Unit.RESOURCE_PATH + name + "/");
        String scriptPath = this.getDirectoryPath() + Unit.SCRIPT_NAME;
        this.activateScript(scriptPath);
        this.initialiseFromScript();
        this.finalizeLoading();

    }

    /** Update the player's state for a frame.
     * @param world A reference to the world.
     * @param input The input object, used to take the keyboard inputs.
     * @param delta Time passed since last frame (milliseconds).
     */
    @Override
    public void update(Input input, int delta) 
    {

        if (this.isAlive()) {
            this.updateFromScript(input, delta);
            if (!this.equals(this.getWorld().getPlayer()))
            {
                this.updateDefault(delta);
            }
        } else {
            this.checkDieing(false, 0, 0);
        }

        super.update(input, delta);

    }

    private void updateDefault(int delta)
    {

        if (this.isAlive()) {

            this.setCoolDown(this.getCoolDown() - delta);
	    if (this.getCoolDown()<0) { this.setCoolDown(0); }
	    this.setTempX(0);
	    this.setTempY(0);

            if (!this.isDieing()) {

                ArrayList<Unit> units = this.getWorld().getAllUnits();
                int len = units.size() - 1;

                for (int i = len ; i >= 0 ; i--) {

                    Unit u = units.get(i);

                    if (this.isEnemy(u)) {

                        boolean busy = this.checkUnitToFight(u, delta);
                        
                        if (!this.isAttacking() && busy) {
	    			this.move(this.getTempX()*delta, this.getTempY()*delta);
	    		} else {
	    			this.idle(delta);
	    		}

                    }
                    else if (this.isAlly(u) && (this.getWorld().getPlayer() == u))
                    {
                        
                    }

                }

                if (!this.isAttacking()) {
                    this.setAttackingStatus(false);
                    if (!this.isMoving()) {
                        this.idle(delta);
                    }
                }



            } else {

                if (this.isRevivingAllowed() && this.isReviveAndTeleport()) {
                    this.checkDieing(true, this.getReviveX(), this.getReviveY());
                }

            }

        } else {

            if (this.isTeleporting()) {
                this.teleport(this.getReviveX(), this.getReviveY());
            } else {
                this.revive();
            }

        }

    }

}